package com.sx.art2.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.sx.art2.beans.Adress;
import com.sx.art2.beans.AuctionInfo;
import com.sx.art2.beans.Order;
import com.sx.art2.beans.User;
import com.sx.art2.mapper.OrderMapper;
import com.sx.art2.service.AdressService;
import com.sx.art2.service.ArtService;
import com.sx.art2.service.AuctionInfoService;
import com.sx.art2.service.OrderService;
@Controller
@RequestMapping("/Order")
@SessionAttributes(types={AuctionInfo.class})
public class OrderController {
	
	@Autowired
	OrderService orderService;
	@Autowired
	ArtService artService;
	@Autowired
	AuctionInfoService auctionInfoService;
	@Autowired
	AdressService adressService;
	
	@RequestMapping("/selectOrderByUser")
	public ModelAndView selectOrderByUser(HttpSession session){
		ModelAndView mv =new ModelAndView();
		User user=(User)session.getAttribute("User");
		List<Order> order=orderService.selectOrderByUser(user.getUserId());
		mv.addObject("order", order);
		mv.setViewName("wddd");
		return mv;
	}
	@RequestMapping("/create")
	public ModelAndView create(Order order,HttpSession session) {
		ModelAndView mv=new ModelAndView();
		User user=(User)session.getAttribute("User");
		Date date=new Date();
		SimpleDateFormat dateFormat0 =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat dateFormat =new SimpleDateFormat("yyyyMMddHHmmssSSS");
		order.setOrderId(dateFormat.format(date).trim());
		order.setOrderTime(dateFormat0.format(date));
		order.setOrderState(1);
		order.setUserId(user.getUserId());
		
		orderService.insert(order);
		Adress adress=adressService.selectById(order.getAddressId());
		mv.addObject("order", order);
		mv.addObject("adress",adress);
		mv.setViewName("fukuan");
		return mv;
	}
	@RequestMapping("/selectById")
	public ModelAndView selectById(String orderId) {
		ModelAndView mv=new ModelAndView();
		mv.setViewName("/orderInfo");
		return mv;
	}
	
	
	
	@RequestMapping("/order")
	public ModelAndView order() {
		
		ModelAndView mv=new ModelAndView();
		List<Order> list =orderService.selectAll();
		mv.addObject("list", list);
		mv.setViewName("admin/Order_handling");
		return mv;
	}
	@RequestMapping("/orderinfo")
	public ModelAndView orderinfo(String orderId) {		
		ModelAndView mv=new ModelAndView();
		Order order =orderService.selectById(orderId);
		mv.addObject("order", order);
		mv.setViewName("admin/order_detailed");
		return mv;
	}
	@RequestMapping("/amounts")
	public ModelAndView amounts() {
		
		ModelAndView mv=new ModelAndView();
		float smoney=0;float tmoney=0;
		List<Order> list =orderService.selectOrderAmount();
		for(int i=0;i<list.size();i++)
		{
			if(list.get(i).getOrderState()==2||list.get(i).getOrderState()==3||list.get(i).getOrderState()==4)
			{
				smoney=smoney+list.get(i).getOrderPrice();
				SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
				String today=df.format(new Date());
				System.out.println(list.get(i).getOrderTime().matches(today));
			}
				
		}
		mv.addObject("smoney", smoney);
		mv.addObject("list", list);
		mv.setViewName("admin/Amounts");
		return mv;
	}
	//发货
	@RequestMapping("/fahuo")
	@ResponseBody
	public String fahuo(String orderId) {
		String data="success";
		SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time=df.format(new Date());
		try {
			orderService.updateOrderState(3, orderId,time);
		} catch (Exception e) {
			// TODO: handle exception
			data="error";
		}
		return data;
	}
	@RequestMapping("/payment")
	public ModelAndView payment(String orderId) {
		
		ModelAndView mv=new ModelAndView();
		orderService.payment(orderId);
		mv.setViewName("orderInfo");
		return mv;
	}
	@RequestMapping("/confirm")
	public ModelAndView confirm(String orderId) {
		
		ModelAndView mv=new ModelAndView();
		SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time=df.format(new Date());
		orderService.confirm(4, orderId, time);
		mv.setViewName("redirect:/Order/selectOrderByUser");
		return mv;
	}
	
	@RequestMapping("/getAddress")
	public ModelAndView getAddress(HttpSession session,int auctionInfoId,int sign) {
		
		ModelAndView mv=new ModelAndView();
		AuctionInfo auctionInfo=auctionInfoService.getArt(auctionInfoId);
		mv.addObject("auctionInfo", auctionInfo);
		User user=(User)session.getAttribute("User");
		System.out.println(user.getUserId());
		List<Adress> list =adressService.list(user.getUserId());		
		mv.addObject("sign", sign);
		mv.addObject("adressList", list);
		mv.setViewName("jiesuan2");
		return mv;
	}
	@RequestMapping("/auctionOrder")
	public ModelAndView auctionOrder(HttpSession session,int auctionInfoId,int sign) {
		
		ModelAndView mv=new ModelAndView();
		auctionInfoService.auctionComfirm(auctionInfoId, 1);
		AuctionInfo auctionInfo=auctionInfoService.getArt(auctionInfoId);
		mv.addObject("auctionInfo", auctionInfo);
		User user=(User)session.getAttribute("User");
		List<Adress> list =adressService.list(user.getUserId());		
		mv.addObject("sign", sign);
		mv.addObject("adressList", list);
		mv.setViewName("jiesuan2");
		return mv;
	}
	@Scheduled(cron="0/1 * *  * * ? ")
	public void scheduled() {
		orderService.withdrawOrder();
	}

}
